from django.shortcuts import render
import time

# Create your views here.
from django.http import HttpResponse


# http:127.0.0.1:8000/api01
def api01(request):
    """
    测试locust压测接口响应时间
    :param request:
    :return:
    """
    time.sleep(4)
    return HttpResponse("1234567890")


# http:127.0.0.1:8000/set_cookie
def set_cookie(request):
    """
    设置cookie，需要一个HttpResponse对象:HttpResponse,HttpResponseRedirect,JsonResponse
    :param request:
    :return:
    """
    response = HttpResponse("设置cookie12")
    response.set_cookie(key="cookie", value="this is cookie")  # value不能包含中文
    return response


# http:127.0.0.1:8000/get_cookie
def get_cookie(request):
    """
    获取cookie
    :param request:
    :return:
    """
    cookie_value = request.COOKIES["cookie"]
    return HttpResponse(cookie_value)


# http:127.0.0.1:8000/set_session
def set_session(request):
    """
    设置session
    :param request:
    :return:
    """
    request.session["username"] = 'smart'
    request.session["age"] = 18
    return HttpResponse("设置session")


# http:127.0.0.1:8000/get_session
def get_session(request):
    """
    获取session
    :param request:
    :return:
    """
    username = request.session["username"]
    age = request.session["age"] = 18
    return HttpResponse(f"{username}---{age}")
